<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="jQuery.min.js"></script>
    <style>
        table {
            width: 400px;
            margin: 100px auto;
        }

        table thead {
            background-color: skyblue;
        }

        table tbody tr {
            cursor: pointer;
        }
    </style>
</head>

<body>
    <table border="1" cellspacing="0">
        <thead>
            <tr>
                <th>全选<input type="checkbox" class="allCheck"></th>
                <th>商品</th>
                <th>价钱</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td><input type="checkbox" class="check"></td>
                <td>iPhone8</td>
                <td>8000</td>
            </tr>
            <tr>
                <td><input type="checkbox" class="check"></td>
                <td>iPhone8</td>
                <td>8000</td>
            </tr>
            <tr>
                <td><input type="checkbox" class="check"></td>
                <td>iPhone8</td>
                <td>8000</td>
            </tr>
            <tr>
                <td><input type="checkbox" class="check"></td>
                <td>iPhone8</td>
                <td>8000</td>
            </tr>
            <tr>
                <td>全选<input type="checkbox" class="allCheck"></td>
                <td>iPhone8</td>
                <td>8000</td>
            </tr>
        </tbody>
    </table>

    <script>
        $(function () {
            $(".allCheck").change(function () {
                // $(this).prop("checked")
                $(".check, .allCheck").prop("checked", $(this).prop("checked"));
            });
            $(".check").change(function () {
                // console.log($(".check:checked").length);
                if ($(".check:checked").length === $(".check").length) {  //:checked可以获得被选中的个数
                    $(".allCheck").prop("checked", true);
                } else {
                    $(".allCheck").prop("checked", false);
                }
            })
        })
    </script>
</body>

</html>